// component/moneySearch/moneySearch.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    myCom: {
      type: Boolean,
      value: false,
    },
    status: {
      type: Number,
      value: 1,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    calendarShow: false,
    minDate: new Date(2024, 1, 1).getTime(),
    maxDate: new Date().getTime() + 1000 * 60 * 60 * 24,
    form: {
      name: '',
      tel: '',
      reviewTel: '',
      // type: '',
      updateTime: '',
      createTime: '',
      affiliated: '',
      createTime1: '',
      updateTime1: '',
      status: '',
      isDelivery: '',
      // fan: '',
      // door: '',
      // tube: '',
      // inversion: '',
      // transfer: '',
      // window: '',
      // pack: '', // 打包类目
      // brandId: '', // 品牌
      // materialId: '', // 型号
      // thicknessId: '', // 壁厚
      // openId: '', // 开启
      money: '', //装修数量
      asterisk: '',
    },
    index: '',
    date: '',
    date1: '',
    isCreate: true,
    typeIndex: '',
    searchIndex: '',
    isDeliveryIndex: '',
    asteriskIndex: '',
    deptOption: [],
    typeData: [],
    selectData: [
      {
        'id': 0,
        'statusName': '综合办'
      },
      {
        'id': 1,
        'statusName': '交接处'
      },
      {
        'id': 2,
        'statusName': '工程部'
      },
      {
        'id': 3,
        'statusName': '已完成'
      }
    ],
    isDeliveryData: [
      {
        value: 0,
        name: '未交付',
      },
      {
        value: 1,
        name: '已交付',
      }
    ],
    asteriskOption: [
      {
        value: '0',
        name: '未激活',
      },
      {
        value: '1',
        name: '已激活',
      }
    ],
    list: [
      { name: '打包类目', status: 0, listUrl: '/api/category/selPackList' },
      { name: '品牌', status: 1, listUrl: '/api/category/selBrandList' },
      { name: '型号', status: 2, listUrl: '/api/category/selMaterialList' },
      { name: '壁厚', status: 3, listUrl: '/api/category/selThicknessList' },
      { name: '开启', status: 4, listUrl: '/api/category/selOpenList' },
    ],
    packIndex: '', // 打包类目index
    packOption: [],
    brandIndex: '', // 品牌index
    brandOption: [],
    materialIndex: '', // 型号index
    materialOption: [],
    thicknessIndex: '', // 壁厚index
    thicknessOption: [],
    openIndex: '', // 开启
    openOption: [],
  },

  created() {
    this.getDishuDepartment();
    this.getTypeData();
    this.data.list.forEach(item => {
      this.getList(item.listUrl, item.status);
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getDishuDepartment() {
      wx.request({
        url: `${app.globalData.baseUrl}/api/finance/getDictData`,
        method: "GET",
        header: {
          'token': wx.getStorageSync('token'),
          "Content-Type": "application/json"
        },
        data: {
          dictType: 'sys_affiliated_unit',
        },
        success: (res) => {
          if (res.data.code == 0) {
            this.setData({
              deptOption: res.data.data,
            });
          }
        }
      })
    },
    // 获取工单类型
    getTypeData() {
      wx.request({
        url: `${app.globalData.baseUrl}/api/finance/getDictData`,
        method: "GET",
        header: {
          'token': wx.getStorageSync('token'),
          "Content-Type": "application/json"
        },
        data: {
          dictType: 'sys_finance',
        },
        success: (res) => {
          if (res.data.code == 0) {
            this.setData({
              typeData: res.data.data,
            });
          }
        }
      })
    },
    getList(url, status) {
      wx.request({
        url: `${app.globalData.baseUrl}${url}`,
        method: "GET",
        header: {
          'token': wx.getStorageSync('token'),
          "Content-Type": "application/json"
        },
        success: (res) => {
          if (res.data.code == 0) {
            const arr = res.data.rows;
            arr.forEach(item => {
              if (status == 2) {
                item.dictCode = item.materialId;
                item.dictLabel = item.materialName;
              } else {
                item.dictCode = item.id;
                item.dictLabel = item.name;
              }
            });
            if (status == 0) this.setData({ packOption: arr });
            if (status == 1) this.setData({ brandOption: arr });
            if (status == 2) this.setData({ materialOption: arr });
            if (status == 3) this.setData({ thicknessOption: arr });
            if (status == 4) this.setData({ openOption: arr });

          } else {
            wx.showToast({
              title: res.data.msg || '出错啦！',
              icon: 'error',
              duration: 2000
            })
          }
        }
      })
    },
    reviewTelInput(e) {
      this.setData({
        form: {
          ...this.data.form,
          reviewTel: e.detail
        }
      });
    },
    fanInput(e) {
      this.setData({
        form: {
          ...this.data.form,
          fan: e.detail
        }
      });
    },
    doorInput(e) {
      this.setData({
        form: {
          ...this.data.form,
          door: e.detail
        }
      });
    },
    tubeInput(e) {
      this.setData({
        form: {
          ...this.data.form,
          tube: e.detail
        }
      });
    },
    inversionInput(e) {
      this.setData({
        form: {
          ...this.data.form,
          inversion: e.detail
        }
      });
    },
    transferInput(e) {
      this.setData({
        form: {
          ...this.data.form,
          transfer: e.detail
        }
      });
    },
    windowInput(e) {
      this.setData({
        form: {
          ...this.data.form,
          window: e.detail
        }
      });
    },
    nameInput(e) {
      this.setData({
        form: {
          ...this.data.form,
          name: e.detail
        }
      });
    },
    typeChange(e) {
      var mchObj = this.data.typeData[e.detail.value];
      this.setData({
        typeIndex: e.detail.value,
        form: {
          ...this.data.form,
          type: mchObj.dictValue
        }
      })
    },
    telInput(e) {
      this.setData({
        form: {
          ...this.data.form,
          tel: e.detail
        }
      });
    },
    moneyInput(e) {
      this.setData({
        form: {
          ...this.data.form,
          money: e.detail
        }
      });
    },
    statusChange: function (e) {
      var mchObj = this.data.selectData[e.detail.value];
      this.setData({
        index: e.detail.value,
        form: {
          ...this.data.form,
          status: mchObj.id
        }
      })
    },
    packChange(e) {
      var mchObj = this.data.packOption[e.detail.value];
      this.setData({
        packIndex: e.detail.value,
        form: {
          ...this.data.form,
          pack: mchObj.dictCode
        }
      })
    },
    brandChange(e) {
      var mchObj = this.data.brandOption[e.detail.value];
      this.setData({
        brandIndex: e.detail.value,
        form: {
          ...this.data.form,
          brandId: mchObj.dictCode
        }
      })
    },
    materialChange(e) {
      var mchObj = this.data.materialOption[e.detail.value];
      this.setData({
        materialIndex: e.detail.value,
        form: {
          ...this.data.form,
          materialId: mchObj.dictCode
        }
      })
    },
    thicknessChange(e) {
      var mchObj = this.data.thicknessOption[e.detail.value];
      this.setData({
        thicknessIndex: e.detail.value,
        form: {
          ...this.data.form,
          thicknessId: mchObj.dictCode
        }
      })
    },
    openChange(e) {
      var mchObj = this.data.openOption[e.detail.value];
      this.setData({
        openIndex: e.detail.value,
        form: {
          ...this.data.form,
          openId: mchObj.dictCode
        }
      })
    },
    deptChange(e) {
      var mchObj = this.data.deptOption[e.detail.value];
      this.setData({
        searchIndex: e.detail.value,
        form: {
          ...this.data.form,
          affiliated: mchObj.dictValue
        }
      })
    },
    isDeliveryChange(e) {
      var mchObj = this.data.isDeliveryData[e.detail.value];
      this.setData({
        isDeliveryIndex: e.detail.value,
        form: {
          ...this.data.form,
          isDelivery: mchObj.value
        }
      })
    },
    asteriskChange(e) {
      var mchObj = this.data.asteriskOption[e.detail.value];
      this.setData({
        asteriskIndex: e.detail.value,
        form: {
          ...this.data.form,
          asterisk: mchObj.value
        }
      })
    },
    onDisplay() {
      this.setData({ calendarShow: true, isCreate: false });
    },
    onDisplay1() {
      this.setData({ calendarShow: true, isCreate: true });
    },
    onRangeClose() {
      this.setData({ calendarShow: false });
    },
    formatDate(date) {
      date = new Date(date);
      let month = date.getMonth() + 1;
      let day = date.getDate();
      if (month < 10) {
        month = '0' + month;
      }
      if (day < 10) {
        day = '0' + day;
      }
      return `${date.getFullYear()}-${month}-${day}`;
    },
    onConfirm(event) {
      const [start, end] = event.detail;
      const endTime = new Date(end).getTime();
      if (this.data.isCreate) {
        this.setData({
          calendarShow: false,
          form: {
            ...this.data.form,
            createTime1: `${this.formatDate(start)} 00:00:00`,
            updateTime1: `${this.formatDate(endTime)} 23:59:59`,
          },
          date1: `${this.formatDate(start)} - ${this.formatDate(end)}`,
        });
      } else {
        this.setData({
          calendarShow: false,
          form: {
            ...this.data.form,
            createTime: `${this.formatDate(start)} 00:00:00`,
            updateTime: `${this.formatDate(endTime)} 23:59:59`,
          },
          date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
        });
      }
    },
    reset() {
      this.setData({
        form: {
          name: '',
          tel: '',
          reviewTel: '',
          createTime1: '',
          updateTime1: '',
          money: '',
          // type: '',
          createTime: '',
          updateTime: '',
          affiliated: '',
          status: '',
          isDelivery: '',
          // fan: '',
          // door: '',
          // tube: '',
          // inversion: '',
          // transfer: '',
          // window: '',
          // pack: '', // 打包类目
          // brandId: '', // 品牌
          // materialId: '', // 型号
          // thicknessId: '', // 壁厚
          // openId: '', // 开启
          asterisk: '',
        },
        index: '',
        date: '',
        date1: '',
        isCreate: true,
        typeIndex: '',
        searchIndex: '',
        isDeliveryIndex: '',
        asteriskIndex: '',
        packIndex: '', // 打包类目index
        brandIndex: '', // 品牌index
        materialIndex: '', // 型号index
        thicknessIndex: '', // 壁厚index
        openIndex: '', // 开启
      });
      this.triggerEvent('resetIsPage');
      this.triggerEvent('search', {
        form: this.data.form,
        nameObj: {
          packName: '',
          brandName: '',
          materialName: '',
          thicknessName: '',
          openName: '',
        }
      });
    },
    search() {
      const data = { ...this.data.form };
      this.triggerEvent('search', {
        form: data,
        nameObj: {
          packName: this.data.packOption[this.data.packIndex]?.dictLabel,
          brandName: this.data.brandOption[this.data.brandIndex]?.dictLabel,
          materialName: this.data.materialOption[this.data.materialIndex]?.dictLabel,
          thicknessName: this.data.thicknessOption[this.data.thicknessIndex]?.dictLabel,
          openName: this.data.openOption[this.data.openIndex]?.dictLabel,
          affiliatName: this.data.deptOption[this.data.searchIndex]?.dictLabel,
          typeName: this.data.selectData[this.data.index]?.statusName,
          deliveryName: this.data.isDeliveryData[this.data.isDeliveryIndex]?.name,
          asteriskName: this.data.asteriskOption[this.data.asteriskIndex]?.name,
        }
      });
    },
    onClose() {
      this.triggerEvent('onClose');
    }
  }
})
